package pl.spikes.xmpp;


import java.io.IOException;
import java.util.Collection;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.muc.MultiUserChat;

import pl.spikes.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TextView;

public class XMPPPrepareGameActivity extends Activity implements
		OnClickListener {
		TableLayout playerTable;
		TextView playerMessage;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.xmpp_prepare_to_start);
		playerTable = (TableLayout) findViewById(R.id.PlayerTable);
		playerMessage = (TextView) findViewById(R.id.player_message);
		String room = "trongame@conference.vps01.fit3140hack.org";
		MultiUserChat tron_game_group = new MultiUserChat(XMPPLogic.getInstance().getConnection(), room);
		createRoom(tron_game_group);
		receiveMessage(tron_game_group);
		
	}

	public void receiveMessage(MultiUserChat tron_game_group) {
		tron_game_group.addMessageListener(new PacketListener() {
            public void processPacket(Packet packet) {
            	ObjectMapper mapper = new ObjectMapper();
            	Player player;
            	try {
					player = mapper.readValue(((Message)packet).getBody(), Player.class);
					System.out.println(player.playerName);
					System.out.println(player.commond);
					playerMessage.append(player.playerName + ":" + player.commond);
	                System.out.println(((Message)packet).getBody());
	                Log.i("message", ((Message)packet).getBody());
				} catch (JsonParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JsonMappingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	
            }

        });
	}
	
	public void createRoom(MultiUserChat tron_game_group) {
		
		try {
//			tron_game_group.create("test1");
			tron_game_group.join("test1");
			tron_game_group.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		Roster roster = XMPPLogic.getInstance().getConnection().getRoster();
//		Collection<RosterEntry> entries = roster.getEntries();
//		for (RosterEntry entry : entries) {
//		    System.out.println(entry);
//		}
		tron_game_group.invite("leoli@vps01.fit3140hack.org", "Meet me in this excellent room");
		
	}
	
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

}
